public class LinkedListQueue<E> implements Queue<E> {
    private class Node {
        public E item;
        public Node next;

        public Node(E element, Node next) {
            this.item = element;
            this.next = next;
        }

        public Node(E element) {
            this(element, null);
        }

        public Node() {
            this(null, null);
        }
    }

    private Node head, tail;
    private int size;

    public LinkedListQueue() {
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 向队列末尾中添加元素——入队
     * @param element 要添加的元素
     */
    @Override
    public void enqueue(E element) {
        if (tail == null) {
            tail = new Node(element);
            head = tail;
        } else {
            tail.next = new Node(element);
            tail = tail.next;
        }
        size++;
    }

    /**
     * 删掉队列头部的元素——出队
     * @return 返回被删掉的元素
     */
    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        }
        Node removedNode = head;
        head = head.next;
        removedNode.next = null;
        if (head == null) {
            tail = null;
        }
        size--;
        return removedNode.item;
    }

    @Override
    public E getHead() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        }
        return head.item;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("LinkedListQueue head {");
        for (Node currentNode = head; currentNode!=null ; currentNode=currentNode.next) {
            sb.append(currentNode.item + "->");
        }
        sb.append("NULL} tail");
        return sb.toString();
    }
}
